﻿using GradientPathLib;
using Microsoft.Msagl.Core.Geometry.Curves;
using Microsoft.Msagl.Core.Layout;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Timtopia.DGMLPosterView;
using MNode = Microsoft.Msagl.Core.Layout.Node;

namespace UnitTestProject1
{
    /// <summary>
    /// Interaction logic for DebugViewer.xaml
    /// </summary>
    public partial class DebugViewer : UserControl
    {
        public static void ViewRouting(GeometryGraph graph, double scale = 1)
        {
            // The dispatcher thread
            var t = new Thread(() =>
            {
                var window = new Window();
                var dv = new DebugViewer(graph);
                dv.ScaleTransform.ScaleX = scale;
                dv.ScaleTransform.ScaleY = scale;
                window.Content = dv;

                // Initiates the dispatcher thread shutdown when the window closes
                window.Closed += (s, e) => window.Dispatcher.InvokeShutdown();

                window.Show();

                // Makes the thread support message pumping
                System.Windows.Threading.Dispatcher.Run();
            });

            // Configure the thread
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
        }
        public DebugViewer(GeometryGraph graph)
        {
            InitializeComponent();
            foreach (var v in graph.Nodes) AddNode(v);
            foreach (var c in graph.RootCluster.AllClustersDepthFirst().Where(c => c!=graph.RootCluster))
            {
                AddNode(c, null);
            }
            foreach (var e in graph.Edges) AddEdge(e);
        }

        private void AddEdge(Edge e)
        {
            var g = e.ToGeometry();
            GradientPath gp = new GradientPath();
            gp.StrokeThickness = 1;
            gp.GradientMode = GradientMode.Parallel;
            var stops = new GradientStopCollection();
            stops.Add(new GradientStop(Colors.Black, 0));
            stops.Add(new GradientStop(Colors.Orange, 1));
            gp.GradientStops = new GradientStopCollection(stops);
            gp.Data = g;
            gp.Tolerance = 0.001;
            Canvas.Children.Add(gp);
        }

        private void AddNode(MNode v)
        {
            AddNode(v, Brushes.Orange);
        }

        private void AddNode(MNode v, Brush fill)
        {
            var rr = v.BoundaryCurve as RoundedRect;
            var r = new Rectangle
            {
                Width=v.Width,
                Height=v.Height,
                Fill=fill,
                Stroke = Brushes.Black,
                RadiusX = rr.RadiusX,
                RadiusY = rr.RadiusY
            };
            Canvas.Children.Add(r);
            Canvas.SetLeft(r, v.BoundaryCurve.BoundingBox.Left);
            Canvas.SetTop(r, v.BoundaryCurve.BoundingBox.Bottom);
        }
    }
}
